﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Vo;
using DoNet.Bpmn.Models;
using DoNet.Bpmn.Services;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnNode
{
    /// <summary>
    /// NodePropertyLevelAdp Class for Level Node Properties
    /// </summary>
    public class NodePropertyLevelAdaptor : BpmnNodeAdaptor
    {
        private readonly BpmnNodeAssignLevelConfigService _bpmnNodeAssignLevelConfigService;
        private readonly ILogger<NodePropertyLevelAdaptor> _logger;

        public NodePropertyLevelAdaptor(
            BpmnNodeAssignLevelConfigService bpmnNodeAssignLevelConfService,
            ILogger<NodePropertyLevelAdaptor> logger)
        {
            _bpmnNodeAssignLevelConfigService = bpmnNodeAssignLevelConfService;
            _logger = logger;
        }

        public override BpmnNodeVo? FormatToBpmnNodeVo(BpmnNodeVo bpmnNodeVo)
        {
            BpmnNodeAssignLevelConfig bpmnNodeAssignLevelConf = _bpmnNodeAssignLevelConfigService.GetByBpmnNodeId(bpmnNodeVo.Id);

            if (bpmnNodeAssignLevelConf != null)
            {
                bpmnNodeVo.Property = new BpmnNodePropertysVo
                {
                    AssignLevelType = bpmnNodeAssignLevelConf.AssignLevelType,
                    AssignLevelGrade = bpmnNodeAssignLevelConf.AssignLevelGrade
                };
            }

            return bpmnNodeVo;
        }


        public override void EditBpmnNode(BpmnNodeVo bpmnNodeVo)
        {
            BpmnNodePropertysVo bpmnNodePropertysVo = bpmnNodeVo.Property ?? new BpmnNodePropertysVo();

            BpmnNodeAssignLevelConfig bpmnNodeAssignLevelConf = new BpmnNodeAssignLevelConfig
            {
                BpmnNodeId = bpmnNodeVo.Id,
                AssignLevelType = bpmnNodePropertysVo.AssignLevelType,
                AssignLevelGrade = bpmnNodePropertysVo.AssignLevelGrade ?? 0,
                CreatorTime = DateTime.Now,
                //CreatorUserId = SecurityUtils.GetLogInEmpName(),
                LastModifyTime = DateTime.Now,
                //LastModifyUserId = SecurityUtils.GetLogInEmpName()
            };

            _bpmnNodeAssignLevelConfigService.Insert(bpmnNodeAssignLevelConf);
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(BpmnNodeAdpConfigEnum.ADP_CONF_NODE_PROPERTY_LEVEL);
        }
    }
}
